#include <boost/interprocess/ipc/message_queue.hpp>   
#include <iostream>   
#include <vector>   

#include "msg_type.h"

using namespace boost::interprocess;  

int main ()  
{  
   try{  
      #if 0
      /*Open a message queue.   */
      message_queue mq  
         (open_only        //only create   
         ,"message_queue"  //name   
         );
         #endif
        #if 1
        /*Erase previous message queue   */
        message_queue::remove("message_queue");  
        /*Create a message_queue.  */
        message_queue mq  
            (create_only               //only create   
            ,"message_queue"           //name   
            ,def_TEST_CNT                       //max message number   
            //,sizeof(int)               //max message size   
            ,sizeof(MSG_DATA)               //max message size   
            
            );
            #endif  

  
      unsigned int priority;  
      message_queue::size_type recvd_size;  
  
      /*Receive 100 numbers   */
      //for(int i = 0; i < def_TEST_CNT; ++i)
      int i=0;
      while(true)
      {  
         MSG_DATA tmpbuf;  
         //mq.receive(&tmpbuf, sizeof(MSG_DATA), recvd_size, priority);
         bool blrcv=mq.try_receive(&tmpbuf, sizeof(MSG_DATA), recvd_size, priority);

         if(false==blrcv )  
         {
            //perror("message recv error:blrcv=%d\n",blrcv);
            printf("message recv error:blrcv=%d\n",blrcv);
            sleep(10);
            continue;  
         }
         
         if(false==blrcv || recvd_size != sizeof(MSG_DATA))  
         {
            //perror("message recv error:blrcv=%d\n",blrcv);
            printf("message recv error:blrcv=%d\n",blrcv);
            return 1;  
         }

        std::cout <<"tmpbuf.type: " <<tmpbuf.mtype <<"recvd_size: " <<recvd_size <<"i: " <<i << std::endl;
        i++;    
      }  
   }  
   catch(interprocess_exception &ex){  
      message_queue::remove("message_queue");  
      std::cout <<"exp: " <<ex.what() << std::endl;  
      return 1;  
   }  
   message_queue::remove("message_queue");  
   return 0;  
}  

